<?php

/**
 * This is the model class for table "insurance".
 *
 * The followings are the available columns in table 'insurance':
 * @property integer $id
 * @property string $policynum
 * @property string $policy_dateissued
 * @property string $policy_dateexp
 * @property integer $client_id
 * @property integer $insurance_company_id
 * @property integer $insurance_type_id
 * @property string $insureditems
 * @property string $termprice
 * @property string $policy_coverage
 * @property string $attachment
 *
 * The followings are the available model relations:
 * @property Client $client
 * @property InsuranceCompany $insuranceCompany
 * @property InsuranceType $insuranceType
 * @property Payment[] $payments
 */
class Insurance extends CActiveRecord
{
	public function getInsType()
	{
		return $this->insuranceType.type_name;
	}
	
	public function getClients()
	{
		return CHtml::listData(Client::model()->findAll(), 'id', 'Names');
	}
	
	public function getInsuranceCo()
	{
		return CHtml::listData(InsuranceCompany::model()->findAll(), 'id', 'company_name');
	}
	
	public function getInsuranceType()
	{
		return CHtml::listData(InsuranceType::model()->findAll(), 'id', 'type_name');
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Insurance the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'insurance';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('policynum, policy_dateissued, policy_dateexp, client_id, insurance_company_id, insurance_type_id', 'required'),
			array('client_id, insurance_company_id, insurance_type_id', 'numerical', 'integerOnly'=>true),
			array('policynum', 'length', 'max'=>20),
			array('insureditems, policy_coverage', 'length', 'max'=>45),
			array('termprice', 'length', 'max'=>8),
			array('attachment', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, policynum, policy_dateissued, policy_dateexp, client_id, insurance_company_id, insurance_type_id, insureditems, termprice, policy_coverage, attachment', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'client' => array(self::BELONGS_TO, 'Client', 'client_id'),
			'insuranceCompany' => array(self::BELONGS_TO, 'InsuranceCompany', 'insurance_company_id'),
			'insuranceType' => array(self::BELONGS_TO, 'InsuranceType', 'insurance_type_id'),
			'payments' => array(self::MANY_MANY, 'Payment', 'insurance_has_payment(insurance_id, payment_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'policynum' => 'Policy Number',
			'policy_dateissued' => 'Date Issued',
			'policy_dateexp' => 'Insurance Expiration',
			'client_id' => 'Client Name',
			'insurance_company_id' => 'Insurance Company',
			'insurance_type_id' => 'Insurance Type',
			'insureditems' => 'Insured Items',
			'termprice' => 'Term Price',
			'policy_coverage' => 'Policy Coverage',
			'attachment' => 'Attachment',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('policynum',$this->policynum,true);
		$criteria->compare('policy_dateissued',$this->policy_dateissued,true);
		$criteria->compare('policy_dateexp',$this->policy_dateexp,true);
		$criteria->compare('client_id',$this->client_id);
		$criteria->compare('insurance_company_id',$this->insurance_company_id);
		$criteria->compare('insurance_type_id',$this->insurance_type_id);
		$criteria->compare('insureditems',$this->insureditems,true);
		$criteria->compare('termprice',$this->termprice,true);
		$criteria->compare('policy_coverage',$this->policy_coverage,true);
		$criteria->compare('attachment',$this->attachment,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}